package controllers

import (
	"dockter/models"
	beego "github.com/beego/beego/v2/server/web"
)

type DockTerController struct {
	beego.Controller
}
type JsonRes struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *DockTerController) List() {
	page, _ := c.GetInt("p", 1)
	pageSize, _ := c.GetInt("s", 3)
	name := c.GetString("name")
	var total int64
	query := models.DB.Model(&models.DockTer{})
	if name != "" {
		query = query.Where("name like ?", "%"+name+"%")
	}
	err := query.Count(&total).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "总数查询失败",
			Data: nil,
		}
		c.ServeJSON()
	}
	var dockTer []models.DockTer
	err = query.Order("fens desc").Offset((page - 1) * pageSize).Limit(pageSize).Find(&dockTer).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "分页失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		JsonData := make(map[string]interface{})
		JsonData["dockTer"] = dockTer
		JsonData["total"] = total
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "分页成功",
			Data: JsonData,
		}
		c.ServeJSON()
	}
}
func (c *DockTerController) Update() {
	//id, _ := c.GetInt("id")
	//var
}
